package com.ruoyi.machine.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;

import com.ruoyi.machine.common.SnowFlake;
import com.ruoyi.machine.domain.Dto.EExpressMachinesDto;
import com.ruoyi.machine.domain.EExpressBoxes;
import com.ruoyi.machine.domain.EExpressMachineInfo;
import com.ruoyi.machine.domain.Vo.EExpressMachinesVo;
import com.ruoyi.machine.mapper.EExpressBoxesMapper;
import com.ruoyi.machine.mapper.EExpressMachineInfoMapper;
import com.ruoyi.machine.util.BoxCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.machine.mapper.EExpressMachinesMapper;
import com.ruoyi.machine.domain.EExpressMachines;
import com.ruoyi.machine.service.IEExpressMachinesService;

/**
 * 柜机管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class EExpressMachinesServiceImpl implements IEExpressMachinesService 
{
    @Autowired
    private EExpressMachinesMapper eExpressMachinesMapper;
    @Autowired
    private EExpressMachineInfoMapper expressMachineInfoMapper;

    /**
     * 查询柜机管理
     * 
     * @param id 柜机管理主键
     * @return 柜机管理
     */
    @Override
    public EExpressMachines selectEExpressMachinesById(Long id)
    {
        return eExpressMachinesMapper.selectEExpressMachinesById(id);
    }

    /**
     * 查询柜机管理列表
     * 
     * @param eExpressMachines 柜机管理
     * @return 柜机管理
     */
    @Override
    public List<EExpressMachines> selectEExpressMachinesList(EExpressMachines eExpressMachines)
    {
        return eExpressMachinesMapper.selectEExpressMachinesList(eExpressMachines);
    }

    /**
     * 新增柜机管理
     * 
     * @param eExpressMachines 柜机管理
     * @return 结果
     */
    @Override
    public int insertEExpressMachines(EExpressMachines eExpressMachines)
    {
        eExpressMachines.setBoxStatus(0L);
        eExpressMachines.setMachineStatus(4L);
        eExpressMachines.setIsDel(0L);
        eExpressMachines.setSystemVersion("MoOS 1.0.1");
        eExpressMachines.setMac("DC179H9908");
        return eExpressMachinesMapper.insertEExpressMachines(eExpressMachines);
    }

    /**
     * 修改柜机管理
     * 
     * @param eExpressMachines 柜机管理
     * @return 结果
     */
    @Override
    public int updateEExpressMachines(EExpressMachines eExpressMachines)
    {
        return eExpressMachinesMapper.updateEExpressMachines(eExpressMachines);
    }

    /**
     * 批量删除柜机管理
     * 
     * @param ids 需要删除的柜机管理主键
     * @return 结果
     */
    @Override
    public int deleteEExpressMachinesByIds(Long[] ids)
    {
        return eExpressMachinesMapper.deleteEExpressMachinesByIds(ids);
    }

    /**
     * 删除柜机管理信息
     * 
     * @param id 柜机管理主键
     * @return 结果
     */
    @Override
    public int deleteEExpressMachinesById(Long id)
    {
        return eExpressMachinesMapper.deleteEExpressMachinesById(id);
    }

    @Override
    public void updateStatus(EExpressMachines machines) {
        EExpressMachines findById= eExpressMachinesMapper.findById(machines.getId());
//        判断柜机状态,如果是未开机状态,则修改状态,添加开启时间
        if (findById.getMachineStatus()==4){
            findById.setOpenDate(DateUtils.getNowDate());
            findById.setMachineStatus(0L);
        }else {
            findById.setMachineStatus(machines.getMachineStatus());
        }
        eExpressMachinesMapper.updateEExpressMachines(findById);
    }

    @Override
    public EExpressMachinesDto findMachineById(Long id) {
        return eExpressMachinesMapper.findAllById(id);
    }

    @Override
    public EExpressMachineInfo findMachineByIdqq(Long id) {
        return eExpressMachinesMapper.findAllByIdqq(id);
    }

    @Override
    public Integer count(EExpressMachines eExpressMachines) {
        return eExpressMachinesMapper.count(eExpressMachines);
    }




    @Autowired
    private EExpressBoxesMapper expressBoxesMapper;
    @Override
    public void save(List<EExpressMachines> list) {
        for (EExpressMachines machine : list) {
            /*生成sn*/
            long sn = SnowFlake.nextId();
            /*赋值属性*/
            machine.setSn(String.valueOf(sn));
            /*添加机柜*/
            eExpressMachinesMapper.save(machine);
            /*根据sn查找机柜*/
            EExpressMachines machines= eExpressMachinesMapper.selectMachineBySn(machine.getSn());
            EExpressMachineInfo info = new EExpressMachineInfo();
            /*赋值属性*/
            info.setMachineId(machines.getId());
            /*添加柜机信息*/
            expressMachineInfoMapper.add(info);
            /*批量添加箱格*/
            int i = machines.getId().intValue();
            List<EExpressBoxes> boxes=addBoxes(i,194,380,110);
            expressBoxesMapper.insertBatch(boxes);
        }
    }

    @Override
    public List<EExpressMachines> list(EExpressMachinesVo vo) {
        return eExpressMachinesMapper.list(vo);
    }

    private static List<EExpressBoxes> addBoxes(Integer machineId, Integer totalWidth, Integer totalHeight, Integer totalDepth) {

        List<EExpressBoxes> expressBoxes = new ArrayList<>();
        int boxNumber = 1; // 箱格编号起始值

        // 假设箱格类型分布：6个大箱，7个中箱，7个小箱
        int[] boxTypes = {2, 1, 0}; // 对应大箱、中箱、小箱的类型值

        for (int type : boxTypes) {
            int count = (type == 2) ? 6 : 7; // 大箱6个，中箱和小箱各7个
            for (int i = 0; i < count; i++) {
                if (expressBoxes.size() >= 20) break; // 如果已经达到20个箱格，退出循环
                EExpressBoxes box = new EExpressBoxes();
                box.setMachineId(machineId); // 柜机ID
                box.setBoxNumber(boxNumber++);
                box.setIsType(type);
                box.setStorageType(type == 2 ? 1 : 0); // 大箱的storageType为1，中箱和小箱的storageType为0
                int temperature = -18;
                BigDecimal bigDecimal = new BigDecimal(temperature);
                box.setTemperature(type == 2 ? bigDecimal : null); // 大箱的temperature为-18，中箱和小箱的temperature为null
                expressBoxes.add(box);
            }
        }

        // 如果箱格数量不足20个，随机添加直到满足条件
        while (expressBoxes.size() < 20) {
            EExpressBoxes box = new EExpressBoxes();
            box.setMachineId(machineId);
            box.setBoxNumber(boxNumber++);
            int randomType = (int) (Math.random() * boxTypes.length); // 随机选择箱格类型
            box.setIsType(boxTypes[randomType]);
            box.setStorageType(boxTypes[randomType] == 2 ? 1 : 0); // 设置storageType
            int temperature = -18;
            BigDecimal bigDecimal = new BigDecimal(temperature);
            box.setTemperature(boxTypes[randomType] == 2 ? bigDecimal : null); // 设置temperature
            expressBoxes.add(box);
        }

        // 使用 BoxCalculator 计算箱格尺寸和位置信息
        BoxCalculator.calculateBoxDimensionsAndPosition(expressBoxes, totalWidth, totalHeight, totalDepth);
        return expressBoxes;
    }
}
